package com.ly.heart.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.OfficeSuppliesRecordEntity;
import com.ly.heart.mapper.OfficeSuppliesMapper;
import com.ly.heart.service.IOfficeSuppliesRecordService;
import com.ly.heart.service.IOfficeSuppliesService;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.OfficeSuppliesEntity;

import javax.annotation.Resource;

/**
 * 办公用品Service业务层处理
 *
 * @author cxl
 * @date 2024-06-09
 */
@Service
public class OfficeSuppliesServiceImpl extends ServiceImpl<OfficeSuppliesMapper,OfficeSuppliesEntity> implements IOfficeSuppliesService {

    @Resource
    private OfficeSuppliesMapper officeSuppliesMapper;
    @Resource
    private IOfficeSuppliesRecordService officeSuppliesRecordService;

    /**
     * 查询办公用品
     *
     * @param id 办公用品ID
     * @return 办公用品
     */
    @Override
    public OfficeSuppliesEntity selectOfficeSuppliesEntityById(String id)
    {
        OfficeSuppliesEntity officeSuppliesEntity = this.getById(id);
        //获取采购记录数量
        List<OfficeSuppliesRecordEntity> purchaseList = officeSuppliesRecordService.list(new QueryWrapper<OfficeSuppliesRecordEntity>().eq("office_supplies_id", id).eq("type", 0));
        officeSuppliesEntity.setPurchaseNum(purchaseList.size());
        //入库记录数量
        List<OfficeSuppliesRecordEntity> warsehouseingList = officeSuppliesRecordService.list(new QueryWrapper<OfficeSuppliesRecordEntity>().eq("office_supplies_id", id).eq("type", 1));
        officeSuppliesEntity.setWarehousingNum(warsehouseingList.size());
        //出库记录数量
        List<OfficeSuppliesRecordEntity> outboundList = officeSuppliesRecordService.list(new QueryWrapper<OfficeSuppliesRecordEntity>().eq("office_supplies_id", id).eq("type", 2));
        officeSuppliesEntity.setOutboundNum(outboundList.size());
        //退库记录数量
        List<OfficeSuppliesRecordEntity> returningList = officeSuppliesRecordService.list(new QueryWrapper<OfficeSuppliesRecordEntity>().eq("office_supplies_id", id).eq("type", 3));
        officeSuppliesEntity.setReturningNum(returningList.size());
        return officeSuppliesEntity;
    }

    /**
     * 查询办公用品列表
     *
     * @param officeSuppliesEntity 办公用品
     * @return 办公用品
     */
    @Override
    public List<OfficeSuppliesEntity> selectOfficeSuppliesEntityList(OfficeSuppliesEntity officeSuppliesEntity)
    {
        return officeSuppliesMapper.selectOfficeSuppliesEntityList(officeSuppliesEntity);
    }

    /**
     * 新增办公用品
     *
     * @param officeSuppliesEntity 办公用品
     * @return 结果
     */
    @Override
    public int insertOfficeSuppliesEntity(OfficeSuppliesEntity officeSuppliesEntity)
    {
        officeSuppliesEntity.setId(SnowflakeIdWorker.getInstance().nextStringId());
        officeSuppliesEntity.setCreateTime(DateUtils.getNowDate());
        officeSuppliesEntity.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        return officeSuppliesMapper.insertOfficeSuppliesEntity(officeSuppliesEntity);
    }

    /**
     * 修改办公用品
     *
     * @param officeSuppliesEntity 办公用品
     * @return 结果
     */
    @Override
    public int updateOfficeSuppliesEntity(OfficeSuppliesEntity officeSuppliesEntity)
    {
        officeSuppliesEntity.setChangeTime(DateUtils.getNowDate());
        return officeSuppliesMapper.updateOfficeSuppliesEntity(officeSuppliesEntity);
    }

    /**
     * 批量删除办公用品
     *
     * @param ids 需要删除的办公用品ID
     * @return 结果
     */
    @Override
    public int deleteOfficeSuppliesEntityByIds(String[] ids)
    {
        return officeSuppliesMapper.deleteOfficeSuppliesEntityByIds(ids);
    }

    /**
     * 删除办公用品信息
     *
     * @param id 办公用品ID
     * @return 结果
     */
    @Override
    public int deleteOfficeSuppliesEntityById(String id)
    {
        return officeSuppliesMapper.deleteOfficeSuppliesEntityById(id);
    }

}
